package com.future.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.future.enums.StatusCode;
import com.future.po.*;
import com.future.service.*;
import com.future.utils.WebUtil;
import com.future.vo.IllegalImgVo;
import com.future.vo.QueryInfoVo;
import com.future.vo.ResponseResultVo;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "监控的API接口")
@RestController
@CrossOrigin
@RequestMapping("/monitor")
public class MonitorController {

    @Resource
    private YSOpenService ysOpenService;

    @Resource
    private MonitorOfPublicService monitorOfPublicService;

    @Resource
    private MonitorOfDrivingService monitorOfDrivingService;

    @Resource
    private PublicStatisService publicStatisService;

    @Resource
    private DrivingStatisService drivingStatisService;

    private QueryInfoVo queryInfoVo;

    @ApiOperation(value = "获取监控access_token", notes = "主要用于监控视频接入")
    @GetMapping("/getAccessToken")
    public ResponseResultVo getMonitorToken() throws Exception {
        String str = ysOpenService.getAccessToken();
        System.out.println(str);
        return ResponseResultVo.success(str);
    }

    @ApiOperation(value = "添加非法监控图片及非法信息", notes = "通过萤石云平台抓取监控图片，保存至磁盘，并调用百度AI接口进行对图片进行判断，操作")
    @ApiImplicitParam(name = "scene", value = "应用场景", required = true)
    @PostMapping("/{scene}")
    public ResponseResultVo postMonitor(@PathVariable String scene, HttpSession session) throws Exception {
        User user = (User) session.getAttribute("user");
        ResponseResultVo result = new ResponseResultVo();

        switch (scene) {
            case "public":
                monitorOfPublicService.monitor(user);
                break;
            case "driving":
                String tmp = monitorOfDrivingService.monitor(user);
                result.setResult(JSONObject.parseObject(tmp));
                result.setCode(200);
                result.setMessage("success");
                break;
            default:
                return ResponseResultVo.failure(StatusCode.BAD_REQUEST, "不存在应用场景" + scene);
        }
        return result;
    }

    @ApiOperation(value = "获取非法监控图片及非法信息列表", notes = "根据session中的user参数获取当前用户的所有非法监控图片及非法信息列表")
    @ApiImplicitParam(name = "scene", value = "应用场景", required = true)
    @PostMapping("/getRecord/{scene}")
    public ResponseResultVo getMonitor(@PathVariable String scene,@RequestBody QueryInfoVo queryInfo) {
        this.queryInfoVo = queryInfo;

        User user = (User) WebUtil.getRequest().getSession().getAttribute("user");
        if (user==null){
             user = new User();
             user.setId(queryInfoVo.getUserId());
        }

        Map<String, Object> map = new HashMap<>();
        switch (scene) {
            case "public":
                List<IllegalImgVo> publicImgVos = monitorOfPublicService.findImgsAll(user);
                map.put("scene", "公共场所");
                map.put("illegalImgList", publicImgVos);
                return ResponseResultVo.success(map);
            case "driving":
                List<IllegalImgVo> drivingImgVos = monitorOfDrivingService.findImgsAll(user,queryInfo);
                setPath(drivingImgVos);
                map.put("scene", "驾驶行为");
                map.put("illegalImgList", drivingImgVos);

                return ResponseResultVo.success(map);
            default:
                return ResponseResultVo.failure(StatusCode.BAD_REQUEST, "不存在应用场景" + scene);
        }
    }

    private void setPath(List<IllegalImgVo> drivingImgVos) {
        for (IllegalImgVo il : drivingImgVos) {
            il.setPath(il.getPath().replaceAll("C:\\\\images\\\\monitor\\\\driving\\\\", "\\\\photo\\\\monitor\\\\"));
        }
    }

    @ApiOperation(value = "获取非法统计信息", notes = "根据session中的user参数获取当前用户的非法统计信息")
    @ApiImplicitParam(name = "scene", value = "应用场景", required = true)
    @GetMapping("/statis/{scene}")
    public ResponseResultVo getStatis(@PathVariable String scene,@RequestParam(required = false) Integer id) {
        User user = (User) WebUtil.getRequest().getSession().getAttribute("user");
        if (user == null){
            user = new User();
            user.setId(id);
        }

        switch (scene) {
            case "public":
                PublicStatis publicStatis = publicStatisService.getOne(new QueryWrapper<PublicStatis>().eq("user_id", user.getId()));
                return ResponseResultVo.success(publicStatis);
            case "driving":
                DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

                Date date = new Date();
                 DrivingStatis drivingStatis = drivingStatisService.getOne(new QueryWrapper<DrivingStatis>().eq("user_id", user.getId()).orderByDesc("date").last( "limit 1"));
                return ResponseResultVo.success(drivingStatis);
            default:
                return ResponseResultVo.failure(StatusCode.BAD_REQUEST, "不存在应用场景" + scene);
        }
    }
}
